package com.show.business.device.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.show.business.common.utils.DateUtils;
import com.show.business.device.bean.*;
import com.show.business.device.bean.vo.AlarmTmpVo;
import com.show.business.device.mapper.UartTmpMapper;
import com.show.business.device.service.*;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Uart设备告警数据传输 服务实现类
 * </p>
 *
 * @author dvlp
 * @since 2024-10-20
 */
@Service
public class UartTmpServiceImpl extends ServiceImpl<UartTmpMapper, UartTmp> implements IUartTmpService {

    @Override
    public List<UartTmp> getList() {
        return baseMapper.getList();
    }

    @Override
    public void saveUartTmp(UartTmp uartTmp){
        baseMapper.insert(uartTmp);
    }

    @Override
    public void updateUartTmpMark(String id){
        baseMapper.updateUartTmpMark(id);
    }

    @Override
    public AlarmTmpVo getAlarmByImei(String imei){
        List<AlarmTmpVo> alarmList=baseMapper.getAlarmByImei(imei);
        AlarmTmpVo alarmTmpVo=null;
        if(alarmList!=null && alarmList.size()>0){
            alarmTmpVo=alarmList.get(0);
            //1、获取最新一条告警信息； 2、获取当前时间，减去35分钟时间 3、当前时间-35 > 告警时间: 离线;排除通信状态
            //优化代码 使用Java 8时间API替代Date
            LocalDateTime currentDate = LocalDateTime.now().minusMinutes(35);//当前时间减去30分钟时间
            //告警时间
            LocalDateTime alarmDate = alarmTmpVo.getAlarmTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();

            // 4. 设置设备状态
            alarmTmpVo.setDeviceStatus(currentDate.isAfter(alarmDate) ? "0" : "1");

            return alarmTmpVo;
        }else{
            return null;
        }
    }
}
